package com.qf.quduba.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.quduba.VO.ResultVO;
import com.qf.quduba.entity.Books;
import com.qf.quduba.entity.Chapter;
import com.qf.quduba.entity.Type;
import com.qf.quduba.service.BookInfoService;
import com.qf.quduba.service.BookService;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 17312
 * @time2020/10/1114:54
 */
@RestController
@CrossOrigin
@RequestMapping("/books")
public class BookController {
    @Resource
    private BookService bookService;
    @Resource
    private BookInfoService bookInfoService;
    @Resource
    private RestHighLevelClient restHighLevelClient;

    @PostMapping("updateHits")
    public ResultVO updateHits(@RequestBody String bookId){
        String replace = bookId.replace("=", "");
        int i = bookService.updateHits(Integer.parseInt(replace));
        if(i > 0){
            return new ResultVO(1000,"成功",null);
        }else{
            return new ResultVO(1001,"失败",null);
        }

    }

    @GetMapping("hot")
    public ResultVO getHotBook(@RequestParam(value = "page", defaultValue = "1", required = false)Integer page,@RequestParam(value = "pageSize", defaultValue = "20", required = false)Integer pageSize){
        PageHelper.startPage(page, pageSize); // 使用PageHelper插件实现分页效果
        List<Books> hotBook = bookService.getHotBook();
        PageInfo pageInfo = new PageInfo(hotBook); // 使用PageInfo对象封装查询结果
        if(hotBook != null && hotBook.size() > 0){
            return new ResultVO(1000,"成功",pageInfo);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }
    @GetMapping("bookInfo")
    public ResultVO getBookInfo(String bookId){

        Books bookInfo = bookInfoService.findBookInfo(bookId);
        if(bookInfo != null){
            return new ResultVO(1000,"成功",bookInfo);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }
    @GetMapping("collect")
    public ResultVO getCollect(){

        List<Books> collect = bookService.getCollect();
        if(collect != null && collect.size() > 0){
            return new ResultVO(1000,"成功",collect);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }
    @GetMapping("update")
    public ResultVO getUpdate(){

        List<Books> update = bookService.getUpdate();
        if(update != null && update.size() > 0){
            return new ResultVO(1000,"成功",update);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }

    @GetMapping("renewal")
    public ResultVO getRenewal(){

        List<Books> renewal = bookService.getRenewal();
        if(renewal != null && renewal.size() > 0){
            return new ResultVO(1000,"成功",renewal);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }

    @GetMapping("creattime")
    public ResultVO getCreattime(){

        List<Books> creattime = bookService.getCreattime();
        if(creattime != null && creattime.size() > 0){
            return new ResultVO(1000,"成功",creattime);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }

    @GetMapping("typeList")
    public ResultVO getTypeList(){

        List<Type> typeList = bookService.getTypeList();
        if(typeList != null && typeList.size() > 0){
            return new ResultVO(1000,"成功",typeList);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }

    @GetMapping("end")
    public ResultVO findHitsEnd(){
        List<Books> booksList = bookService.findHitsEnd();
        if(booksList != null && booksList.size() > 0){
            return new ResultVO(1000,"成功",booksList);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }
    @GetMapping("getread")
    public ResultVO getchapterId(int chapterId){

        Chapter getread = bookInfoService.findchapterInfo(chapterId);
        if(getread != null){
            return new ResultVO(1000,"成功",getread);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }

    @PutMapping("updateCollect")
    public ResultVO updateCollect(String bookId){
        int i = bookService.updateCollect(bookId);
        if(i != 0){
            return new ResultVO(1000,"成功",null);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }

    @GetMapping("getBooksData")
    public ResultVO getBooksData(@RequestParam(value = "page", defaultValue = "1", required = false)Integer page,@RequestParam(value = "pageSize", defaultValue = "4", required = false)Integer pageSize,@RequestParam Integer userId){
        PageHelper.startPage(page, pageSize); // 使用PageHelper插件实现分页效果
        List<Books> booksData = bookService.getBooksData(userId);
        PageInfo pageInfo = new PageInfo(booksData); // 使用PageInfo对象封装查询结果
        if(pageInfo != null){
            return new ResultVO(1000,"success",pageInfo);
        }else{
            return new ResultVO(1001,"fail",null);
        }
    }
    @GetMapping("getAggregate")
    public ResultVO getAggregate(@RequestParam Integer userId){
        int aggregate = bookService.getAggregate(userId);
        if(aggregate != 0){
            return new ResultVO(1000,"成功",aggregate);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }

    //修改书籍的打赏量
    @PutMapping("updateReward")
    public ResultVO updateReward(String bookId,Double money){
        int i = bookService.updateReward(bookId,money);
        if(i != 0){
            return new ResultVO(1000,"成功",null);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }

    @PostMapping("addBook")
    public ResultVO addBook(@RequestBody Books books){
        try {
            int i = bookService.addBook(books);
            ObjectMapper objectMapper = new ObjectMapper();
            IndexRequest request = new IndexRequest("quduba");
            String jsonStr = objectMapper.writeValueAsString(books);
            request.id(books.getBookId()+"");
            request.source(jsonStr, XContentType.JSON);
            if(i > 0){
                return new ResultVO(1000,"成功",null);
            }else{
                return new ResultVO(1001,"失败",null);
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("updateBook")
    public ResultVO updateBook(@RequestBody Books books){
        try {
            int i = bookService.updateBook(books);
            ObjectMapper objectMapper = new ObjectMapper();
            IndexRequest request = new IndexRequest("quduba");
            String jsonStr = objectMapper.writeValueAsString(books);
            request.id(books.getBookId()+"");
            request.source(jsonStr, XContentType.JSON);
            if(i > 0){
                return new ResultVO(1000,"成功",null);
            }else{
                return new ResultVO(1001,"失败",null);
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

}
